/*
  This file is a part of hashutils package.
  (c) 2004 Zeljko Vrba <zvrba@globalnet.hr>

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <errno.h>
#include <mycrypt.h>
#include "cdt_line.h"

/**
  @file
  CDT discipline for set of lines.
*/

static char rcsid[] = "$Id: cdt_line_set.c 2 2007-03-29 14:43:55Z zeljko.vrba $";

/* TODO: nonportable. Is key always suitably aligned for unsigned int? */
static unsigned int line_hashf(Dt_t *dt, Void_t *key, Dtdisc_t *disc)
{
	return *(unsigned int*)key;
}

Dtdisc_t Cdt_LineDiscipline = {
	offsetof(struct line, h),	/* key */
	0,							/* key size: calculated depending on hash */
	0,							/* link: beginning of struct */
	NULL,						/* no copy method - must be allocated */
	NULL,						/* free */
	NULL,						/* compare: use CDT internal implementation */
	line_hashf,
	NULL,
	NULL
};

int LineSet_report_collisions = 1;
Vmalloc_t *LineSet_vmpool = NULL;

/**
   This function reads an already open file line by line and stores the hash
   of each line into data container (currently, unordered set).
*/
Dt_t *LineSet_read_file(Sfio_t *f, int halg)
{
	char *record;
	Dt_t *set;
	unsigned int recno = 0;

	if(!(set = dtopen(&Cdt_LineDiscipline, Dtset))) {
		sfprintf(sfstderr, "FATAL: memory exhausted. Exiting.\n");
		exit(1);
	}
	while((record = sfgetr(f, '\n', 0))) {
		unsigned long outlen = Cdt_LineDiscipline.size;
		struct line *line, *inserted;
		int err;

		if(!(line = vmalloc(LineSet_vmpool, LINESZ(Cdt_LineDiscipline.size)))) {
			sfprintf(sfstderr, "FATAL: memory exhausted. Exiting.\n");
			exit(1);
		}

		line->n = ++recno;
		if((err = hash_memory(halg, (unsigned char*)record,
							  sfvalue(f), line->h, &outlen)) != CRYPT_OK) {
			sfprintf(sfstderr, "FATAL: crypto error: %s, exiting.\n",
					 error_to_string(err));
			exit(1);
		}

		/* line is last allocated block so it can be freed even with Vmlast */
		if((inserted = dtinsert(set, line)) != line) {
			if(LineSet_report_collisions) {
				sfprintf(sfstderr, "WARNING: hash collision or duplicate lines: %d,%d\n", inserted->n, line->n);
			}
			vmfree(LineSet_vmpool, line);
		}
	}

	if(sferror(f)) {
		sfprintf(sfstderr, "ERROR reading file: sfgetr: %s\n", strerror(errno));
		dtclose(set);
		set = NULL;
	}
	return set;
}
